<?php

/**
 * Controller_header
 * 班主任管理
 * 
 */

class Controller_header extends MyApp {
	public function __construct()
	{

		parent::__construct();

		$this->model->log   = YOU::loadModel('log');
		
	}
	/** 首页 **/
	public function actionIndex()
	{

	}
	/*选择班级*/
	public function actionSel()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` WHERE staff_id='3'");
		if ($_POST) {
			$class_id = $this->post->class_id;
			Helper_Client::reurl("index.php?a=header.seat&class_id={$class_id}");
		}
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('header/header.sel.tpl.php');
	}
	public function actionReg()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		//添加班主任
		if ($_POST) {

			$staff_id    = $this->post->staff_id;
			$staff_name  = $this->post->staff_name;
			$header_time = $this->post->header_time;
			$header_date = $this->post->header_date;
			$header_data = $this->db->fetchRow("SELECT * FROM `tbl_staff` WHERE `staff_id`='{$staff_id}' ");

			if (!empty($header_data)) {
				// 写入数据库
				$info = array(

				"staff_id"              => $staff_id,
				"staff_name"            => $staff_name,
				"header_time"           => $header_time,
				"header_date"           => $header_date,
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);

				$this->db->do_insert("tbl_header", $info);
				Helper_Client::location("任命成功", "index.php?a=header.login");
			}else{
				Helper_Client::location("没有该教师", "index.php?a=header.reg");
			}
		}
		$this->tpl->display('header/header.reg.tpl.php');
	}
	public function actionSeat()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		//班级座号表
		$class_id = (int)$this->get->class_id;

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`  ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$seat_lists   = $this->db->fetchAll("SELECT * FROM `tbl_student` WHERE `class_id`='{$class_id}' ORDER BY `student_seat` ASC LIMIT {$start}, ".PAGE_LIMIT."");

		$this->tpl->assign("class_id", $class_id);

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("seat_lists", $seat_lists);
		$this->tpl->display('seat/seat.list.tpl.php');
	}
	/*班主任确认登陆*/
	public function actionLogin()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		if ($_POST) {

			$staff_id    = $this->post->staff_id;
			$staff_name  = $this->post->staff_name;
			$log_data    = $this->db->fetchRow("SELECT * FROM `tbl_staff` WHERE `staff_id`='{$staff_id}' ");
			if (!empty($log_data)) {
				Helper_Client::location("欢迎登陆", "index.php?a=header.seat&staff_id={$staff_id}");
			}else{
				Helper_Client::location("请重新登陆", "index.php?a=header.login");
			}
		}
		$this->tpl->display('header/header.login.tpl.php');
	}
	/*班级排序      */
	public function actionOrder()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$class_id = (int)$this->get->class_id;
		if ($_POST) {
			foreach ($_POST['student_seat'] as $k => $v) {
				$update = array(
				"student_seat" =>$v,
				);
				$this->db->do_update("tbl_student", $update,"student_id='{$k}' ");
			}
			// 跳转
			Helper_Client::location("排序成功", "index.php?a=header.seat&class_id={$class_id}");

		}
	}

	public function actionEval()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$class_id    = (int)$this->get->class_id;
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`WHERE `class_id`='{$class_id}'");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$student_id="m.student_id=s.student_id";
		$stu_lists = $this->db->fetchAll("SELECT * FROM `tbl_student` as s INNER join `tbl_message` as m   WHERE m.`class_id`= {$class_id} and {$student_id} AND m.`message_type`='1' AND m.`semester_id`='1' ORDER BY  m.`student_id` DESC LIMIT {$start}, ".PAGE_LIMIT."");
		if (empty($class_id)) {
			Helper_Client::location("请选择班级", "index.php?a=header.sel");
		}
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign('stu_lists', $stu_lists);
		$this->tpl->display('header/header.Eval.tpl.php');


	}
	/*更改班干部班主任身份*/
	public function actionUpdate()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);

		$student_id   = (int)$this->get->student_id;
		$seat_data = $this->db->fetchRow("SELECT * FROM `tbl_student` WHERE `student_id`='{$student_id}'");
		if (empty($seat_data)){
			Helper_Client::alert("请通过班主任身份更改");
		}
		if ($_POST) {
			$student_job       = $this->post->student_job;
			if (empty($student_job)) {
				$info = array(
				'student_job'     =>"普遍学生",
				);
				$this->db->do_update("tbl_student", $info, "student_id='{$student_id}'");
				Helper_Client::location("该学生仍为普通学生", "index.php?a=message.sec");
			}
			$str="";
			foreach ($student_job as $value) {
				$str=$str."\n".$value;

			}
			$info=array(
			"student_job"  => $str,
			);
			$this->db->do_update("tbl_student", $info, "student_id='{$student_id}'");
			Helper_Client::location("修改职务成功", "index.php?a=message.sec");
		}
		$leader_lists=$this->db->fetchAll("SELECT `leader_name` FROM `tbl_student_leader`");
		$this->tpl->assign("leader_lists",$leader_lists);
		$this->tpl->assign("seat_data", $seat_data);
		$this->tpl->display('seat/seat.update.tpl.php');
	}
	public function actionDel()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		//删除职务，还原为普通学生
		$student_id = (int)$this->get->student_id;

		$common     = array(
		"student_job"=>"普通学生",
		);
		$this->db->do_update("tbl_student", $common, "student_id='{$student_id}'");
		Helper_Client::location("删除成功", "index.php?a=message.sec");
	}

	public function actionActlist()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$act_lists=$this->db->fetchAll("SELECT * FROM `tbl_activitys` WHERE `activitys_type`='4' and `status`='1'");
		$this->tpl->assign("act_lists",$act_lists);

		$this->tpl->display('header/header.actlist.tpl.php');



		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student`  ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$seat_lists   = $this->db->fetchAll("SELECT * FROM `tbl_student` WHERE `class_id`='{$class_id}' ORDER BY `student_seat` ASC LIMIT {$start}, ".PAGE_LIMIT."");

		$this->tpl->assign("class_id", $class_id);

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("act_lists", $act_lists);
		$this->tpl->display('header/header.actlist.tpl.php');



	}
	public function actionAddact()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		if ($_POST) {
			$activitys_title=$this->post->activitys_title;
			$activitys_date=$this->post->activitys_date;
			$activitys_author=$this->post->activitys_author;
			$activitys_con=$this->post->activitys_con;

			if (empty($activitys_title) || empty($activitys_con)) {
				Helper_Client::alert("所有项目必须填满！");
			}
			// 写入数据库
			$info = array(
			"activitys_type"        => '4',//班级活动
			"activitys_title"       => $activitys_title,
			"activitys_date"        => $activitys_date,
			"activitys_con"         => $activitys_con,
			"activitys_author"      => $activitys_author,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_activitys", $info);
			$activitys_id = $this->db->insert_id();
			// 上传附件
			if (!empty($_FILES['activitys_file']['tmp_name'])){
				$a = explode(".", $_FILES['activitys_file']['name']);
				$ftype = strtolower($a[count($a)-1]);

				$name = "{$activitys_id}.{$ftype}";
				$file_path = UPLOAD_PATH."/teacher_in/{$name}";
				move_uploaded_file($_FILES['activitys_file']['tmp_name'], $file_path);
				$info = array(
				"activitys_file" => $file_path,
				);
				$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
			}

			Helper_Client::location("添加成功", "index.php?a=header.actlist");
		}

		$this->tpl->display('header/header.add.tpl.php');
	}
	/*班级活动修改*/
	public function actionUpact()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_id= $this->get->activitys_id;
		$act_data = $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE activitys_id={$activitys_id}");
		if ($_POST) {
			$activitys_title = $this->post->activitys_title;
			$activitys_date  = $this->post->activitys_date;
			$activitys_author= $this->post->activitys_author;
			$activitys_con   = $this->post->activitys_con;
			if (!empty($_FILES['activitys_file']['tmp_name'])){
				$a = explode(".", $_FILES['activitys_file']['name']);
				$ftype = strtolower($a[count($a)-1]);
				$name = "{$activitys_id}.{$ftype}";
				$file_path = UPLOAD_PATH."/teacher_in/{$name}";
				move_uploaded_file($_FILES['activitys_file']['tmp_name'], $file_path);
				$file = array(
				"activitys_file" => $file_path,
				);
				$this->db->do_update("tbl_activitys", $file, "activitys_id='{$activitys_id}'");
			}
			// 写入数据库
			$info = array(
			"activitys_type"        => '4',//班级活动
			"activitys_title"       => $activitys_title,
			"activitys_date"        => $activitys_date,
			"activitys_con"         => $activitys_con,
			"activitys_author"      => $activitys_author,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
			// 上传附件

			Helper_Client::location("修改成功", "index.php?a=header.actlist");
		}
		$this->tpl->assign('act_data', $act_data);
		$this->tpl->display('header/header.update.tpl.php');
	}
	/** 删除  **/
	public function actionDelact()
	{
		$activitys_id = (int)$this->get->activitys_id;
		$act_data = $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE activitys_id='{$activitys_id}' AND status='1'");

		if (empty($act_data)){
			Helper_Client::alert("活动不存在");
		}

		// 修改status = 2
		$info = array(
		"status" => 2,
		);
		$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");

		// 跳转
		Helper_Client::reurl("index.php?a=header.actlist");

	}
	public function actionActInfo()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_id = (int)$this->get->activitys_id;
		$act_data = $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE activitys_id='{$activitys_id}'");
		if (empty($act_data)){
			Helper_Client::alert("不存在");
		}
		$this->tpl->assign('act_data', $act_data);
		$this->tpl->display('header/header.info.tpl.php');

	}



}

?>